/**
 * 
 */
package com.totobox.apps.common;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Text;

import android.R;
import android.app.Activity;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

/**
 * @author ytslove
 *
 */
public class DownListAdapter extends ArrayAdapter {
	
	public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
	
	private int layout;
	private Activity context;
	List<String> list;
	public List<ProgressBar> pList = new ArrayList<ProgressBar>();
	public List<TextView> tList = new ArrayList<TextView>();
	
	private int cPosition = 0;
	
	@SuppressWarnings("unchecked")
	public DownListAdapter(Activity context , int layout , List<String> list){
		
		super(context , layout , list);
		
		this.context = context;
		this.list = list;
		this.layout = layout;
	}
	
	public View getView(int position , View convertView , ViewGroup parent){
		
		LayoutInflater inflater = context.getLayoutInflater();
		View vw = inflater.inflate(layout, null);
		
		//pList.add((ProgressBar)vw.findViewById(R.id.dn_progress));
		
		new DownloadFileAsyncTask().execute(list.get(position));
		
		return  vw;
	}
	
	class DownloadFileAsyncTask extends AsyncTask<String, String, String> {

		String fileName = "";
		boolean isCanceled = false;
		
		String t_filename;
		String t_file_url;
		int total_curRate = 0;
		private String homeDir = "/sdcard/totofun/";
		
		private EditText txt_file_size;
		
		//private ProgressBar fileSizeProgressBar;
		
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			//showDialog(DIALOG_DOWNLOAD_PROGRESS);
		}

		@Override
		protected String doInBackground(String... strData) {
			int count;
			BufferedInputStream input = null;
			BufferedOutputStream output = null;
			
			try {
				URL url = new URL(t_file_url);
				URLConnection conexion = url.openConnection();
				conexion.connect();
				int lenghtOfFile = conexion.getContentLength(); // downlod the file
				input = new BufferedInputStream(url.openStream());
				output = new BufferedOutputStream(new FileOutputStream(homeDir + t_filename));
				
				byte data[] = new byte[1024];
				long total = 0;
				while ((count = input.read(data)) != -1) {
					total += count;
					publishProgress(""+(int) (total * 100 / lenghtOfFile));
					output.write(data, 0, count);
				}
				output.flush();
				
			} catch (Exception e) {
                e.printStackTrace();
            } finally {
                try{ if( output != null ) output.close(); }catch(Exception ex){}
                try{ if( input != null ) input.close(); }catch(Exception ex){}
            }
			return null;
		}

		@Override
		protected void onCancelled() {
			isCanceled = true;
			new File(homeDir + fileName).delete();
			checkFinishDown();
			//activity.finish();
			super.onCancelled();
		}

		// publishProgress(…) 메소드 호출의 callback
		@Override
		protected void onProgressUpdate(String... progress) {
			pList.get(cPosition).setProgress(Integer.parseInt(progress[0]));
			txt_file_size.setText(progress[0] + "%");
		}

		@Override
		protected void onPostExecute(String unused) {
			checkFinishDown();
		}
	}
	
	public void checkFinishDown() {

	}

}
